package com.stx.category.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.stx.blog.entity.BlogEntity;
import com.stx.category.dao.CategoryDAO;
import com.stx.category.entity.CategoryEntity;
import com.stx.category.vo.CategoryVO;
import com.stx.user.entity.UserEntity;

public class CategoryDAOImpl extends JdbcDaoSupport implements CategoryDAO {

	class BlogRowMapper implements RowMapper {
		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

			BlogEntity blogEntity = new BlogEntity();
			blogEntity.setBlogID(rs.getString("bid"));
			blogEntity.setUserID(rs.getString("userid"));
			blogEntity.setTitle(rs.getString("title"));
			blogEntity.setBlogContent(rs.getString("blogContent"));
			blogEntity.setCid(rs.getString("cid"));
			blogEntity.setReadLevel(rs.getString("readLevel"));
			blogEntity.setReadAble(rs.getString("readAble"));
			blogEntity.setCreateTime(rs.getString("createTime"));
			blogEntity.setDelflag(rs.getString("delflag"));
			blogEntity.setCategoryName(rs.getString("cname"));

			return blogEntity;
		}

	}

	class CategoryRowMapper implements RowMapper {
		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

			CategoryEntity categoryEntity = new CategoryEntity();

			categoryEntity.setCid(rs.getString("cid"));
			categoryEntity.setCategoryName(rs.getString("cName"));
			categoryEntity.setUserID(rs.getString("userID"));
			categoryEntity.setCreateTime(rs.getString("createTime"));

			return categoryEntity;
		}

	}

	@Override
	public CategoryEntity getCategoryByCid(String cID) {
		JdbcTemplate jt = getJdbcTemplate();
		String sql = "select * from categorytable where cid = ?";
		Object[] args = { cID };
		CategoryEntity categoryEntity = new CategoryEntity();
		categoryEntity = (CategoryEntity) jt.queryForObject(sql, args,
				new CategoryRowMapper());
		return categoryEntity;
	}

	@Override
	public List<CategoryEntity> listCategory() {

		String sql = "select * from categorytable";

		JdbcTemplate jt = getJdbcTemplate();

		List<CategoryEntity> categoryList = jt.query(sql,
				new CategoryRowMapper());

		return categoryList;
	}

	@Override
	public List<CategoryEntity> getCategoryByUserID(String userID) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addCategory(CategoryVO categoryvo) {

		JdbcTemplate jt = getJdbcTemplate();
		Date date = new Date(new Date().getTime());
		String currentTime = date.getYear() + 1900 + "/"
				+ (date.getMonth() + 1) + "/" + date.getDate();

		String sql = "insert into categorytable values(cid.nextval,?,?,?)";
		Object[] args = { categoryvo.getCategoryName(), categoryvo.getUserID(),
				currentTime };
		jt.update(sql, args);

	}

	@Override
	public void deleteCategory(String cID) {

		JdbcTemplate jt = getJdbcTemplate();

		String sql = "delete from categorytable where cid = ?";

		// 构建参数数组
		Object[] args = { cID };

		jt.update(sql, args);

	}

	@Override
	public void editCategory(String cID, String cName) {
		JdbcTemplate jt = getJdbcTemplate();
		String sql = "update categorytable set cname= ? where cid = ?";
		Object[] args = { cName, cID };
		jt.update(sql, args);
	}

	@Override
	public List<BlogEntity> listBlogBycategoryID(String cid) {

		String sql = "select * from bloggertable b,categorytable c where b.cid='"
				+ cid + "'and b.cid=c.cid";
		JdbcTemplate jt = getJdbcTemplate();

		List<BlogEntity> blogList = jt.query(sql, new BlogRowMapper());

		return blogList;

	}

}
